/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe.util;

import java.io.Serializable;
import java.util.Enumeration;

import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-11-3下午05:44:17
 */
public class CommonFiledChecker implements Serializable {
	
	private static final long serialVersionUID = -7953473518346788943L;

	public static String checkParamValue(String id,String fieldName,PortletRequest portletRequest){
		String value = portletRequest.getParameter(fieldName);
		if(value==null){
			CommonFieldValidator.addErrorNotice(id, fieldName, CommonFieldValidator.REQUEST_PARAM_NULL, portletRequest.getPortletSession());
		}else{
			if(value.equals("")){
				CommonFieldValidator.addErrorNotice(id, fieldName, CommonFieldValidator.REQUEST_PARAM_EMPTY, portletRequest.getPortletSession());
			}else{
				setFieldValue(id,fieldName,value,portletRequest);
			}
		}
		return value;
	}
	
	private static final String COMMON_FIELD_VALUE_=CommonFiledChecker.class.getName()+"_COMMON_FIELD_VALUE_";
	
	public static void setFieldValue(String id,String fieldName,String value,PortletRequest portletRequest){
		portletRequest.getPortletSession().setAttribute(COMMON_FIELD_VALUE_+id+"_"+fieldName, value);
	}

	public static String getFieldValue(String id,String fieldName,String value,PortletRequest portletRequest){
		String tValue = value;
		Object valueObj =  portletRequest.getPortletSession().getAttribute(COMMON_FIELD_VALUE_+id+"_"+fieldName);
		if((valueObj!=null)&&(valueObj instanceof String)){
			tValue = (String)valueObj;
		}
		return tValue;
	}

	public static void clearFieldValue(String id,PortletSession portletSession){
		Enumeration attNames = portletSession.getAttributeNames();
		while(attNames.hasMoreElements()){
			Object attNameObj = attNames.nextElement();
			if((attNameObj!=null)&&(attNameObj instanceof String)){
				String attName = (String)attNameObj;
				if(attName.startsWith(COMMON_FIELD_VALUE_+id+"_")){
					portletSession.removeAttribute(attName);
				}
			}
		}
	}

	public static void clearFieldValue(PortletSession portletSession){
		Enumeration attNames = portletSession.getAttributeNames();
		while(attNames.hasMoreElements()){
			Object attNameObj = attNames.nextElement();
			if((attNameObj!=null)&&(attNameObj instanceof String)){
				String attName = (String)attNameObj;
				if(attName.startsWith(COMMON_FIELD_VALUE_)){
					portletSession.removeAttribute(attName);
				}
			}
		}
	}

}
